/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package schoolbank;

import java.util.HashMap;
import java.util.Iterator;
import static schoolbank.SchoolBank.isUser;

/**
 *
 * @author meydlo
 */
public class User {
      
    private String userID;
    private HashMap<String, Account> accounts = new HashMap<String, Account>();
    
    /***************************************************************
     * CONSTRUCTOR
     **************************************************************/
    
    public User(String userID){
        this.userID = userID;
    }
        
    /***************************************************************
     * LOW LEVEL
     **************************************************************/
       
    public String createAccount(String accountID, String accountType){
                
        if(isAccount(accountID))
        {
            return "account ID \""+accountID+"\" already exists";
        }
        else
        {
            try{
                Account a = new Account(accountID, accountType);
                accounts.put(accountID, a);
                return ("account \""+accountID+"\" as account type \""+accountType+"\""
                            + " successfully created: " +
                        (accounts.get(accountID) != null));
            }catch(Exception e){
                return e.getMessage();
            }
        }
    }
    
    public String readAccount(String accountID)
    {
        if(!isAccount(accountID))
        {
            return "account \""+userID+" "+accountID+"\" does not exist";
        }
        else
        {
            Account a = accounts.get(accountID);
            return ("money on account \""+userID+" "+accountID+"\": "+a.getBalance());
        }
    }
    
    public String deleteAccount(String accountID)
    {         
        if(!isAccount(accountID)){                     // account does not exists
            return ("account \""+userID+" "+accountID+"\" does not exist");
        }
        else
        {
            accounts.remove(accountID);                // remove user
       
            return ("user acount \""+userID+" "+accountID+"\" "
                            + "was successfully deleted: "+(!isAccount(userID)));
        }
    }    
    
    public String withdraw(String accountID, double amount)
    {
        if(!isAccount(accountID)){                     // account does not exists
           return ("account \""+userID+" "+accountID+"\" does not exist");
        }
        else{
            double returnVal= accounts.get(accountID).withdraw(amount);
            
            if(returnVal < 0)
                return "unable to withdraw money, \n"+readAccount(accountID);
            else
                return readAccount(accountID);
        }    
    }
    
    public String deposit(String accountID, double amount)
    {
        if(!isAccount(accountID)){                     // account does not exists
           return ("account \""+userID+" "+accountID+"\" does not exist");
        }
        else{
            double returnVal= accounts.get(accountID).deposit(amount);
            
            if(returnVal < 0)
                return "unable to deposit money, \n"+readAccount(accountID);
            else
                return readAccount(accountID);
        }  
    }
    
    /***************************************************************
     * EXTRA
     **************************************************************/
    
    
    public boolean isAccount(String account)
    {
        return accounts.containsKey(account);
    }
        
    /***************************************************************
     * GETTERS / SETTERS
     **************************************************************/
    
    
    public void setUserID(String userID)
    {
        this.userID = userID;
    }
    
    public String getUserID()
    {
        return userID;
    }
    
    public void setAccounts(HashMap<String, Account> accounts)
    {
        this.accounts = accounts;
    }
    
    public HashMap<String, Account> getAccounts()
    {
        return accounts;
    }
    
}
